package summit.tests
{
import flash.events.Event;

import summit.basecamp.helpers.PersonHelper;
import summit.basecamp.helpers.TimeEntryHelper;
import summit.data.AccountToken;
import summit.events.BasecampAPIEvent;
import summit.tests.tools.SummitTestContainer;
import summit.tests.tools.TestHelpers;

public class TimeEntryHelperTests extends SummitTestContainer
{
	private var _timeEntryId:String;
	private var _personId:String;
	
	public function TimeEntryHelperTests(token:AccountToken)
	{
		super(token);
	}
	
	public function readTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(TimeEntryHelper.read(_timeEntryId));
	}
	
	public function listTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(TimeEntryHelper.list(projectId));
	}
	
	public function editUpdateTest(context:Object):void
	{
		initializeLoader(loader_OKEdit);
		loader.load(TimeEntryHelper.edit(_timeEntryId));
	}
	
	private function loader_OKEdit(e:BasecampAPIEvent):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(TimeEntryHelper.update(e.data));	
	}
	
	public function reportTest(context:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(TimeEntryHelper.report());
	}
	
	override protected function self_CompleteInit(e:Event):void
	{
		super.initializeLoader(loader_OKInitPerson);
		loader.load(PersonHelper.read(null));		
	}
	
	private function loader_OKInitPerson(e:BasecampAPIEvent):void
	{
		_personId = e.data.id;
		super.initializeLoader(loader_OKInitPrepare);
		loader.load(TimeEntryHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepare(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreate);
		
		e.data["person-id"] = _personId;
		e.data["date"] = "08/12/2010";
		
		loader.load(TimeEntryHelper.create(e.data));
	}
	
	private function loader_OKInitCreate(e:BasecampAPIEvent):void
	{
		_timeEntryId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.self_CompleteInit(null);	
	}
	
	override public function cleanUp():void
	{
		super.initializeLoader(loader_OKCleanUp);
		loader.load(TimeEntryHelper.destroy(_timeEntryId));
	}
	
	private function loader_OKCleanUp(e:BasecampAPIEvent):void
	{
		super.cleanUp();
	}
}
}